<template>
	<Layout class="p-5 bg-white">
		<Form class="p-5" :model="voucherType" :label-width="150">
			<Row>
				<Col span="8">
				<FormItem class="'w-100'" label="会员券类型名称" >
					<Input style="width: 170px" v-model="voucherType.name" :onkeyup="voucherType.name = voucherType.name.replace(/\s+/g,'')"/>
				</FormItem>
				<FormItem class="'w-100'" label="有效时长/天" >
					<!-- <InputNumber v-model="voucherType.timeLongs" clearable  style="width: 170px;" :parser="value => value.replace('.', '')" ></InputNumber>
					<InputNumber v-model="voucherType.timeLongs" clearable style="width: 170px;"/> -->
					<!-- <InputNumber v-model="voucherType.timeLongs" clearable style="width: 170px;"/> -->
					<Input @keyup.native="changeNumber" v-model="voucherType.timeLongs" placeholder="请输入时长"></Input>
				</FormItem>
				<FormItem class="'w-100'" label="会员券状态">
					<Select  v-model="voucherType.statu" clearable placeholder="请选择会员券状态" style="width: 170px;">
						<Option v-for="item in status" :value="item.value" :key="item.value" >{{item.label}}</Option>
					</Select>
				</FormItem>
				</Col>
			</Row>
			<FormItem>
				<Button @click="submit" type="primary">提交</Button>
				<Button @click="cancel" style="margin-left: 8px">取消</Button>
			</FormItem>
		</Form>
	</Layout>
</template>
<script>
	export default {
		data() {
			return {
				voucherType: {
					voucherTypeId:'',
					name:'',
					timeLongs:'',
					statu : '',
				},
				status: [
					{
						value: 1,
						label: '可用'
					},
					{
						value: 0,
						label: '禁用'
					},
				],
			}
		},
		methods: {
			changeNumber() {
				let str = '' + this.voucherType.timeLongs;      
				if (str.indexOf('.') != -1) {        
					let arr = str.split('');
			        arr.splice(arr.length - 1);        
					let str2 = arr.join('');        
					this.voucherType.timeLongs = +str2;
			      }
			    },
			submit: function() {
				if(this.voucherType.name != null && this.voucherType.name != ""){
					if(this.voucherType.timeLongs != null && this.voucherType.timeLongs != "" && this.voucherType.timeLongs > 0){
						this.http.put({
							url: "/client/voucher-type/one",
							param:{
								voucherTypeId: this.$data.voucherType.voucherTypeId,
								name: this.$data.voucherType.name,
								timeLongs: this.$data.voucherType.timeLongs,
								statu: this.$data.voucherType.statu,
							}
						}).then(data => {
							this.message.info("修改成功");
							this.$tabs.close();
							this.$tabs.open('/voucherType');
						});
					}else{
						this.message.error("时长无效");
					}
				}else{
					this.message.error("会员券类型名称不可为空");
				}
			},
			cancel: function() {
				this.$tabs.close();
				this.$tabs.open('/voucherType');
			},
			init: function() {
				this.search();
			},
			search: function() {
				this.http.get({
					url: "/client/voucher-type/info",
					param: {
						voucherTypeById: this.$data.voucherType.voucherTypeId,
					}
				}).then(data => {
					this.voucherType = data;
					if(this.$data.voucherType.statu){
						this.$data.voucherType.statu = 1;
					}else{
						this.$data.voucherType.statu = 0;
					}
				});
			},
			// change (statu) {
			//     // this.$Message.info('开关状态：' + statu);
			//     }
		},
		created: function() {
			this.$data.voucherType.voucherTypeId = this.$route.params.voucherTypeId;
			
			this.init();
		},
		
		
	}
</script>

